//
//  DatabaseTool.m
//  OrderDish
//
//  Created by MAX on 15-6-15.
//  Copyright (c) 2015年 MAX. All rights reserved...
//

#import "DatabaseTool.h"


@implementation DatabaseTool


static FMDatabase *db = nil;
+(BOOL)openDataBase
{
    if (!db)
    {
        db = [[FMDatabase alloc] initWithPath:[NSHomeDirectory() stringByAppendingPathComponent:@"Documents/UserData.sqlite3"]];
    }
    if (![db open])
    {
        return NO;
    }
    [db shouldCacheStatements];//设置缓存
    return YES;
}
+(void)createTable
{
    NSString *sql = [NSString stringWithFormat:@"create table if not exists SCTEXT (id integer primary key,account text,loginTimes text,signDays text,headIcon text,company text,sex text,loginDays text,cellphone text,icon text,lastSignTime text,balance text,name text,type text,identifier text,birth text,level text,mood text,email text,remarks text,thirdType text,address text,integral text)"];
    if ([self openDataBase])
    {
        if (![db executeQuery:@"SCTEXT"])
        {
            [db executeUpdate:sql];
            NSLog(@"%@",NSHomeDirectory());
            NSLog(@"表创建成功");
        }
        [db close];
    }
}//34
+(void)insertDataWithAccount:(NSString *)account
                       loginTimes :(NSString *)loginTimes
                       signDays:(NSString *)signDays
                       headIcon:(NSString *)headIcon
                       company:(NSString *)company
                       sex:(NSString *)sex
                       loginDays:(NSString *)loginDays
                       cellphone:(NSString *)cellphone
                       icon:(NSString *)icon
                       lastSignTime:(NSString *)lastSignTime
                       balance:(NSString *)balance
                       name:(NSString *)name
                       type:(NSString *)type
                       identifier:(NSString *)identifier
                       birth:(NSString *)birth
                       level:(NSString *)level
                       mood:(NSString *)mood
                       email:(NSString *)email
                       remarks:(NSString *)remarks
                       thirdType:(NSString *)thirdType
                       address:(NSString *)address
                    integral:(NSString *)integral;


{
    NSLog(@"%@",name);
    NSString *sql = @"INSERT INTO SCTEXT (account,loginTimes,signDays,headIcon,company,sex,loginDays,cellphone,icon,lastSignTime,balance,name,type,identifier,birth,level,mood,email,remarks,thirdType,address,integral) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    if ([self openDataBase])
    {
        
            [db executeUpdate:sql,account,loginTimes,signDays,headIcon,company,sex,loginDays,cellphone,icon,lastSignTime,balance,name,type,identifier,birth,level,mood,email,remarks,thirdType,address,integral];
            NSLog(@"数据插入成功");
        [db close];
    }
}
+(void)TableDelete;

{
    NSString *sql = @"delete from SCTEXT where id = 1";
    if ([self openDataBase])
    {
        
        [db executeUpdate:sql];
        NSLog(@"数据插入成功");
        [db close];
    }
}
+(NSMutableArray *)searchTable
{
    NSString *sql = @"SELECT * FROM SCTEXT";
    NSMutableArray * array = nil;
    if ([self openDataBase])
    {
        array = [[NSMutableArray alloc] init];
        FMResultSet *result = [db executeQuery:sql];
        while ([result next])
        {
//            UserData * user = [[UserData alloc]init];
//            user.account   =[result stringForColumn:@"account"];
//            user.loginTimes =[result stringForColumn:@"loginTimes"].doubleValue;
//            user.signDays=[result stringForColumn:@"signDays"].doubleValue;
//            user.headerIconStr=[result stringForColumn:@"headIcon"];
//            user.company=[result stringForColumn:@"company"];
//            user.sex=[result stringForColumn:@"sex"].integerValue;
//            user.loginDays=[result stringForColumn:@"loginDays"].doubleValue;
//            user.cellphone=[result stringForColumn:@"cellphone"];
//            user.icon=[result stringForColumn:@"icon"];
//            user.lastSignTime=[result stringForColumn:@"lastSignTime"];
//            user.balance=[result stringForColumn:@"balance"].doubleValue;
//            user.name=[result stringForColumn:@"name"];
//            user.type=[result stringForColumn:@"type"].doubleValue;
//            user.dataIdentifier=[result stringForColumn:@"identifier"].doubleValue;
//            user.birth=[result stringForColumn:@"birth"];
//            user.level=[result stringForColumn:@"level"].doubleValue;
//            user.mood=[result stringForColumn:@"mood"];
//            user.email=[result stringForColumn:@"email"];
//            user.remarks=[result stringForColumn:@"remarks"];
//            user.thirdType=[result stringForColumn:@"thirdType"].integerValue;
//            user.address=[result stringForColumn:@"address"];
//            user.integral=[result stringForColumn:@"integral"].doubleValue;
//
//            [array addObject:user];
        }
        [result close];
        [db close];
        
    }
    return array;
    
}
+(void)updateMassage:(NSString *)Massage  info:(NSString *)info {
    
    
    NSString *sql = [NSString stringWithFormat:@"UPDATE SCTEXT SET %@ = ?",Massage];
    if ([self openDataBase])
    {
        
        [db executeUpdate:sql,info];
        [db close];
    }

}
@end








